#include "record.h"
#include "includes.h"



#define REC_DATABLOCK_SIZE    (sizeof(RecordData_t))
#define DCB_FLASH_ADDR        (DATA_RECORD_ADDR)
#define REC_ITEMS_MAX_NUM     (10)

static RecordData_t rec;
static RecordDCB_t  dcb;

uint8_t recTempbuf[REC_DATABLOCK_SIZE];

void RecDcbInit()
{
    Flash_Read(DCB_FLASH_ADDR,(uint8_t*)&dcb,sizeof(dcb));
    if(dcb.itemsum >REC_ITEMS_MAX_NUM)
    {
         dcb.itemsum = 0;
         dcb.nextWriteAddress = DCB_FLASH_ADDR+sizeof(dcb);
    }
   
}

//把记录写入flash
void RecWriteFlash(EVT_en e)
{
/*    uint32_t htime;
    uint8_t i;
	uint16_t RecordNum =0x0000;

    rec.id[0] = (uint8_t)(dcb.itemsum);
	rec.id[1] = (uint8_t)(dcb.itemsum >>8);

    rec.rTimeStamp[0] = 0;
    rec.rTimeStamp[1] = 0;
    rec.rTimeStamp[2] = 0;
    rec.rTimeStamp[3] = 0;

    htime = bsp_GetRunTimeSec();
    rec.hTimeStamp[0] = (uint8_t)(htime);
	rec.hTimeStamp[1] = (uint8_t)(htime>>8);
	rec.hTimeStamp[2] = (uint8_t)(htime>>16);
	rec.hTimeStamp[3] = (uint8_t)(htime>>24);

    rec.evtType = e;   
    rec.sumVolt[0] =  (uint8_t)(afeInfo.SumBatteryPackVolt/100 );
	rec.sumVolt[1] = (uint8_t)(afeInfo.SumBatteryPackVolt/100 >>8);

    if(afeInfo.State_RT == 0x02)
    {      
        rec.curr[0] = (uint8_t)afeInfo.ChgCurrent;
		rec.curr[1] = (uint8_t)(afeInfo.ChgCurrent>>8);
		
    }else
    {        
        rec.curr[0] = (uint8_t)afeInfo.DsgCurrent;
		rec.curr[1] = (uint8_t)(afeInfo.DsgCurrent>>8);//放电为负
    }
    

    rec.batHighTemp = (afeInfo.CellTmax-2731) /10;   
    rec.resCap[0] = (uint8_t)((uint16_t)(100 * algEnginer.resCapAH_r));
	rec.resCap[1] = (uint8_t)((uint16_t)(100 * algEnginer.resCapAH_r) >> 8);

    rec.fcc[0] = (uint8_t)((uint16_t)(100 * algIntnel.capacity));
	rec.fcc[1] = (uint8_t)((uint16_t)(100 * algIntnel.capacity) >> 8);
 
    rec.batState[0] = (uint8_t)(afeInfo.State_RT);
	rec.batState[1] = (uint8_t)(afeInfo.State_RT>>8);

   
    

    if(dcb.itemsum == REC_ITEMS_MAX_NUM)
    {
        //做一次fifo移动
        for(i = 0;i <REC_ITEMS_MAX_NUM-1;i++){
            RecordRead(i+1,recTempbuf);
			RecordNum = (((uint16_t)recTempbuf[1] << 8) + recTempbuf[0]);
			recTempbuf[0] = (uint8_t)(RecordNum - 1);
			recTempbuf[1] = (uint8_t)((RecordNum - 1) >> 8);
            Flash_Write(DCB_FLASH_ADDR+sizeof(dcb)+i*sizeof(RecordData_t),recTempbuf,sizeof(RecordData_t));
        }
        Flash_Write(dcb.nextWriteAddress,(uint8_t*)&rec,REC_DATABLOCK_SIZE);

    }else
    {
        Flash_Write(dcb.nextWriteAddress,(uint8_t*)&rec,REC_DATABLOCK_SIZE);
        dcb.nextWriteAddress += REC_DATABLOCK_SIZE;
        dcb.itemsum ++;
    }  
     //写入flash相应的地址  
    Flash_Write(DCB_FLASH_ADDR,(uint8_t*)&dcb,sizeof(dcb));

    */
}

uint8_t RecordRead(uint8_t id,uint8_t *rbuf)
{
/*    uint32_t rtime;
	
    //读取数据到flash
    Flash_Read(DCB_FLASH_ADDR+sizeof(dcb)+id*sizeof(RecordData_t),rbuf,sizeof(RecordData_t));
    rtime = bsp_GetRunTimeSec();

	
    //修改到读时间值   
    rbuf[6] = (uint8_t)(rtime);
	rbuf[7] = (uint8_t)(rtime>>8);
	rbuf[8] = (uint8_t)(rtime>>16);
	rbuf[9] = (uint8_t)(rtime>>24);*/
	
	
    return sizeof(RecordData_t);
}

uint16_t RecordGetItems()
{
    return dcb.itemsum;
}